package net.yxsoft.bean;


import com.jfinal.core.Controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * BootstrapTable分页封装
 * Created by YXLF on 2016/5/6.
 */
public class BSTablePage implements Serializable {

  public static final String PARAM_PAGE_OFFSET = "offset";
  public static final String PARAM_ORDER_MODE = "order";
  public static final String PARAM_ORDER_CONDITION = "orderColunm";
  public static final String PARAM_PAGE_SIZE = "limit";

  /**
   * 分页参数初始化值：默认显示第几页
   */
  public static final int DEFAULT_PAGE_INDEX = 1;

  /**
   * 分页参数初始化值：默认每页显示几多
   */
  public static final int DEFAULT_PAGE_SIZE = 20;

  private static final long serialVersionUID = -76210363296184541L;
  /**
   * 分页查询参数
   */
  private String orderColunm;// 排序条件
  private String orderMode;// 排序方式
  private int pageNumber = DEFAULT_PAGE_INDEX;// 第几页
  private int pageSize = DEFAULT_PAGE_SIZE;// 每页显示几多

  /**
   * 分页结果住数据
   */
  private List<?> list; // 当前页数据
  private int totalPage; // 总页数
  private int totalRow; // 总行数

  /**
   * 分页返回扩展数据
   */
  private Object extData; // 返回扩展数据

  /**
   * 分页显示辅助属性
   */
  private int currentPageCount;// 当前页记录数量
  private boolean isFirst;// 是否第一页
  private boolean isLast;// 是否最后一页

  private String pageStr;    //最终页面显示的底部翻页导航，详细见：getPageStr();
  private boolean entityOrField = true;  //true:需要分页的地方，传入的参数就是Page实体；false:需要分页的地方，传入的参数所代表的实体拥有Page属性

  private HashMap<String, Object> result;

  private BSTablePage() {
  }

  public static BSTablePage init(Controller controller) {
    BSTablePage page = new BSTablePage();

    // 排序条件
    String orderColunm = controller.getPara(BSTablePage.PARAM_ORDER_CONDITION);
    if(null != orderColunm && !orderColunm.isEmpty()) {
      page.setOrderColunm(orderColunm);
    }

    // 排序方式
    String orderMode = controller.getPara(BSTablePage.PARAM_ORDER_MODE);
    if(null != orderMode && !orderMode.isEmpty()) {
      page.setOrderMode(orderMode);
    }

    String pageNumber = controller.getPara(BSTablePage.PARAM_PAGE_OFFSET);
    String pageSize = controller.getPara(BSTablePage.PARAM_PAGE_SIZE);
    int page_index = pageNumber == null ? 1 : Integer.parseInt(pageNumber);
    int page_size = pageSize == null ? 20 : Integer.parseInt(pageSize);

    if(page_index != 0) {// 获取页数
      page_index = page_index / page_size;
    }

    page_index += 1;

    // 第几页
    page.setPageNumber(page_index);

    // 每页显示几多
    page.setPageSize(page_size);

    return page;
  }

  public HashMap<String, Object> getResult() {
    if(result == null) {
      return compute();
    }

    return this.result;
  }

  /**
   * 分页计算并返回结果
   */
  public HashMap<String, Object> compute() {
    if(totalRow == 0) {
      getTotalPage();
    }

    if(pageNumber > totalPage) {
      pageNumber = totalPage;
    }

    this.currentPageCount = (null != this.list ? this.list.size() : 0);// 当前页记录数

    if(pageNumber == 1) {
      this.isFirst = true;
    } else {
      this.isFirst = false;
    }

    if(pageNumber == totalPage) {
      this.isLast = true;
    } else {
      this.isLast = false;
    }

    result = new HashMap<String, Object>() {
      {
        put("rows", list);
        put("page", pageNumber);
        put("total", totalRow);
        put("totalPage", totalPage);

      }
    };

    return result;
  }

  /**
   * 计算总页数
   *
   * @return
   */
  public int getTotalPage() {
    if((this.totalRow % this.pageSize) == 0) {
      this.totalPage = this.totalRow / this.pageSize;// 计算多少页
    } else {
      this.totalPage = this.totalRow / this.pageSize + 1;// 计算多少页
    }
    return totalPage;
  }

  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }

  public int getTotalRow() {
    return totalRow;
  }

  public void setTotalRow(int totalRow) {
    this.totalRow = totalRow;
  }

  public List<?> getList() {
    return list;
  }

  public void setList(List<?> list) {
    this.list = list;
  }

  public String getOrderColunm() {
    return orderColunm;
  }

  public void setOrderColunm(String orderColunm) {
    this.orderColunm = orderColunm;
  }

  public String getOrderMode() {
    return orderMode;
  }

  public void setOrderMode(String orderMode) {
    this.orderMode = orderMode;
  }

  /**
   * 需要查询显示第几页
   *
   * @return
   */
  public int getPageNumber() {
    if(pageNumber <= 0) {
      pageNumber = DEFAULT_PAGE_INDEX;
    }
    return pageNumber;
  }

  public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
  }

  /**
   * 每页显示多少条数据
   *
   * @return
   */
  public int getPageSize() {
    if(pageSize <= 0) {
      pageSize = DEFAULT_PAGE_SIZE;
    }

    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getCurrentPageCount() {
    return currentPageCount;
  }

  public void setCurrentPageCount(int currentPageCount) {
    this.currentPageCount = currentPageCount;
  }

  public boolean isFirst() {
    return isFirst;
  }

  public void setFirst(boolean isFirst) {
    this.isFirst = isFirst;
  }

  public boolean isLast() {
    return isLast;
  }

  public void setLast(boolean isLast) {
    this.isLast = isLast;
  }

  public Object getExtData() {
    return extData;
  }

  public void setExtData(Object extData) {
    this.extData = extData;
  }

  public void setPageStr(String pageStr) {
    this.pageStr = pageStr;
  }

  public boolean isEntityOrField() {
    return entityOrField;
  }

  public void setEntityOrField(boolean entityOrField) {
    this.entityOrField = entityOrField;
  }
}